﻿using System;
using System.Threading.Tasks;
using System.Windows;

namespace ServerTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            cboList.ItemsSource = StrList;
            cboList.SelectedIndex = 0;
            LogHelp.ConfigLog4Net();
            TxtAction = new Action<string>(UpdateInfo);
            LogHelp.AddText(this.TxtAction);
        }

        Action<string> TxtAction;

        log4net.ILog log = log4net.LogManager.GetLogger(typeof(MainWindow));
        private string[] StrList = { "系统考核", "更新天气","浪潮大数据更新" };
        private Task task = null;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //log.Info("test");
            string txt = cboList.Text;
            if (task != null)
            {
                if (task.IsCompleted == false)
                {
                    MessageBoxResult dr = MessageBox.Show("任务正在进行中!", "提示", MessageBoxButton.OK, MessageBoxImage.Question);
                    return;
                }
            }
            task = Task.Factory.StartNew(() =>
            {
                log.Info("-----开始工作:---------");
                try
                {
                    //测试任务
                    MyJob.TestJob testJob = new MyJob.TestJob();
                    testJob.Do();
                }
                catch (Exception ex)
                {
                    UpdateInfo(ex.ToString());
                }

                log.Info("------结束开始工作------");
            }
            );
        }
        private void UpdateInfo(string info)
        {
            this.Dispatcher.BeginInvoke(new Action(
                () =>
                {
                    txtInfo.Text += info;
                })
           );
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            txtInfo.Text = "";
        }
    }
}
